<?php

namespace ulue\core\db\mysql\driver;

use Ulue,
    Debug,
    Mysqli;
use ulue\core\db\base\AbstractDbDriver;
use ulue\core\db\TheDatabaseException;

class MysqliDriver extends AbstractDbDriver
{
	//定义私有化静态变量 保存实例化的对象，默认为空--表示尚未存储
    static private $dbInstance  = NULL;
    protected $STMT;            //预处理（结果集）对象
    protected $connect         = null; //数据库链接

    private $isBeginTrans       = false; //是否开启事务

    public function connect()
    {
    	if ( is_null( self::$dbInstance) )
        {
            $dsn      = $this->connectStr;
            $username = $this->dbUsername;
            $password = $this->dbPassword;

            try {
                self::$dbInstance = new Mysqli($dsn, $username, $password);
            } catch (DatabaseException $e) {
                throw new DatabaseException( 'Mysqli: database connect failed!'.$e->getMessage(),'db' );
            }

        }

        $this->_connect = self::$dbInstance;
        $this->setCharset();//字符集

        return $this->_connect;
    }

    public function setCharset()
    {
        $this->query("SET NAMES {$this->_db['charset']}");
    }

    /**
     * [query description]
     * @param  string $sql [description]
     * @param int $resultMode
     * @return void [type]             [description]
     */
    public function query( $sql , $resultMode = MYSQLI_STORE_RESULT)
    {
        $this->connect->query( $sql , $resultMode);
    }

    public function getOne()
    {

    }

    //释放结果集
    protected function resultFree()
    {
        $this->STMT->free_result();
        $this->STMT = NULL;
    }

    //预处理
    public function prepare($sql)
    {
        return $this->STMT = $this->_connect->prepare($sql);
    }
    /**
     * [bindParam description]
     * @param string $type [i int d double s string b 相应的变量是一个blob和将发送数据包 packets]
     * @param array $param
     * @example
     * $db = Ulue::$app->getDb();
     * $db -> prepare ( "INSERT INTO CountryLanguage VALUES (?, ?, ?, ?)" );
     * $db -> bindParam('isss', [$arg1,$arg2,$arg3,$arg4] );
     * @return [type] [description]
     */
    public function bindParam( $type, $param )
    {
        call_user_func_array( array($this->STMT,'bind_param'), (array) $param);

        return $this;
    }

    public function execute()
    {
        if ($this->STMT) {
            return $this->STMT->execute();
        }else{
            throw new DatabaseException("请先准备好预查询！");
        }
    }

    /**
     * [bindResult 绑定结果到指定变量 ]
     * @todo 未测试
     * @param  $var1,$var2,$var3,$var4... [description]
     * @return [type]        [description]
     */
    public function bindResult()
    {
        call_user_func_array( array($this->STMT,'bind_result'), func_get_args() );

        return $this;
    }

   //开启事务处理
    public function beginTrans()
    {
        if (!$this->isbeginTrans) {
            $this->_connect->begin_transaction();
            $this->isbeginTrans = TRUE;
        }
    }

    //提交(当前)一个事务
    public function commit()
    {
        if ($this->isbeginTrans) {
            $this->_connect->commit();
            $this->isbeginTrans = FALSE;
        }
    }

    //回滚事务
    public function rollback()
    {
        if ($this->isbeginTrans) {
            $this->_connect->rollback();
            $this->isbeginTrans = FALSE;
        }
    }

    public function close()
    {
        $this->_connect->close();
    }
}